package org.jiuling.netty.initializer;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolConfig;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.timeout.IdleStateHandler;
import org.jiuling.basics.validation.Assertions;
import org.jiuling.basics.validation.ObjectVerify;
import org.jiuling.netty.config.IdleStateConfig;
import org.jiuling.netty.config.WebSocketConfig;
import org.jiuling.netty.handler.WebSocketBusinessHandler;
import org.jiuling.netty.handler.WebSocketHeartBeatHandler;
import org.jiuling.netty.handler.service.BusinessHandlerService;
import org.jiuling.netty.handler.service.HeartBeatHandlerService;

import java.util.concurrent.TimeUnit;

/**
 * WebSocket客户端初始化
 * @author 紫灵
 * @date 2025年07月07日 17时53分40秒
 */
public class WebScoketChannelInitializer extends ChannelInitializer<NioSocketChannel> {

    /** WebSocket配置 */
    private final WebSocketConfig webSocketConfig;
    private final IdleStateConfig idleStateConfig;

    public WebScoketChannelInitializer(WebSocketConfig webSocketConfig) {
        this.webSocketConfig = webSocketConfig;
        this.idleStateConfig = IdleStateConfig.builder().build();
    }

    public WebScoketChannelInitializer(WebSocketConfig webSocketConfig,IdleStateConfig idleStateConfig) {
        this.webSocketConfig = webSocketConfig;
        this.idleStateConfig = idleStateConfig;
    }

    @Override
    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
        ChannelPipeline channelPipeline = nioSocketChannel.pipeline()
                .addLast(new HttpServerCodec())
                .addLast(new HttpObjectAggregator(this.webSocketConfig.getMaxContentLength()))
//                .addLast(new WebSocketServerProtocolHandler(this.webSocketConfig.getUrl()))
                .addLast(new WebSocketServerProtocolHandler(
                        WebSocketServerProtocolConfig.newBuilder()
                                .websocketPath(this.webSocketConfig.getUrl())
                                .checkStartsWith(this.webSocketConfig.isCheckStartsWith())
                                .build()
                ));


        // 加载心跳处理器,实现心跳检测
        HeartBeatHandlerService heartBeatHandlerService = this.webSocketConfig.getHeartBeatHandlerService();
        if (!ObjectVerify.isNull(heartBeatHandlerService)){
            channelPipeline.addLast(
                    new IdleStateHandler(
                            this.idleStateConfig.getReaderIdleTime(),
                            this.idleStateConfig.getWriterIdleTime(),
                            this.idleStateConfig.getAllIdleTime(),
                            this.idleStateConfig.getUnit()
                    )
            );
            channelPipeline.addLast(new WebSocketHeartBeatHandler(heartBeatHandlerService));
        }
        // 加载业务处理器
        channelPipeline.addLast(new WebSocketBusinessHandler(this.webSocketConfig.getBusinessHandlerService()));

    }
}
